package com.supertange.miniapp.wepet.modules.wepet.controller;

import com.supertange.miniapp.wepet.common.controller.BaseController;
import com.supertange.miniapp.wepet.common.utils.Assert;
import com.supertange.miniapp.wepet.common.vo.AjaxResult;
import com.supertange.miniapp.wepet.common.utils.MD5Utils;
import com.supertange.miniapp.wepet.modules.wepet.entity.UsersEntity;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.UsersInfoVO;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.UsersVO;
import com.supertange.miniapp.wepet.modules.wepet.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(value = "用户注册登录接口", tags = "注册登录controller")
public class RegistLoginController extends BaseController {
    @Autowired
    UserService userService;


    @PostMapping("/wxRegist")
    @ApiOperation(value = "微信用户注册", notes = "微信用户注册的接口")
    public AjaxResult wxRegist(@RequestBody String userId, String verifyCode){
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(verifyCode)) Assert.isError("请正确填好信息");
        UsersInfoVO usersInfoVO=new UsersInfoVO();
        usersInfoVO.setId(userId);
        String phone=null;
        usersInfoVO.setPhone(phone);
        Assert.isZero(userService.updateUserInfo(usersInfoVO),"更新失败");
        return AjaxResult.ok("注册成功");
    }

    @PostMapping("/regist")
    @ApiOperation(value = "用户注册", notes = "用户注册的接口")
    public AjaxResult regist(@RequestBody UsersEntity user) throws Exception {
        if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword()))
            Assert.isError("用户名和密码不能为空");
        boolean userIsExist = userService.queryUsernameIsExist(user.getUsername());
        //用户名存在
        if (userIsExist) return AjaxResult.errorMsg("用户名已经存在,请换一个");
        user.setNickname(user.getUsername());
        user.setPassword(MD5Utils.getMD5Str(user.getPassword()));
        user.setFansCounts(0);
        user.setReceiveLikeCounts(0);
        user.setFollowCounts(0);
        //存数据
        userService.saveUser(user);
        UsersVO usersVO = setUserRedisSessionToken(user);
        return AjaxResult.ok(usersVO);
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录的接口")
    public AjaxResult login(@RequestBody UsersEntity user) throws Exception {
        String username = user.getUsername();
        String password = user.getPassword();
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return AjaxResult.ok("用户名或密码不能为空");
        }
        UsersEntity userResult = userService.queryUserForLogin(username, MD5Utils.getMD5Str(password));
        if (userResult != null) {
            UsersVO userVO = setUserRedisSessionToken(userResult);
            return AjaxResult.ok(userVO);
        } else {
            return AjaxResult.errorMsg("用户名或密码不正确...");
        }
    }


    @PostMapping("/logout")
    @ApiOperation(value = "用户注销", notes = "用户注销接口")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query")
    public AjaxResult logout(String userId) {
        redisService.del(userId);
        return AjaxResult.ok();
    }

    @PostMapping("/sendMessageCode")
    @ApiOperation(value = "发送验证码", notes = "发送验证码接口")
    public AjaxResult sendMessageCode(String userId) {
        return AjaxResult.ok();
    }



}
